package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.CustomException;
import com.reggie.entity.Category;
import com.reggie.entity.Dish;
import com.reggie.entity.Setmeal;
import com.reggie.mapper.CategoryMapper;
import com.reggie.mapper.DishMapper;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 自定义根据id删除分类方法，删除之前需要进行判断
     * @param fid
     */
    @Override
    public void iremove(Long fid) {
        // 根据当前分类的ID，查询该分类下是否存在菜品，如果存在，则提示错误信息
        //根据条件查询菜品
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,fid);
        Integer count1 = dishMapper.selectCount(lqw);
        if( count1 > 0 ){
            //表示存在
            throw new CustomException("该分类下存在菜品.不能删除!");
        }

        // 根据当前分类的ID，查询该分类下是否存在套餐，如果存在，则提示错误信息
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,fid);
        Integer count2 = setmealMapper.selectCount(lqw2);
        if( count2 > 0 ) {
            //表示存在
            throw new CustomException("该分类下存在套餐.不能删除!");
        }
        categoryMapper.deleteById(fid);
    }
}
